import 'package:flutter/cupertino.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/video/video_play_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/assets.dart';
import 'package:safetymonitor/app/data/entities/application/risk_img_entity.dart';

import 'snap_shot_history_controller.dart';

/// 随手拍历史
class SnapShotHistoryPage extends StatelessWidget {
  SnapShotHistoryPage({super.key});

  final controller = Get.put(SnapShotHistoryController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: const Color(0xFFF7FBFE),
            appBar: CustomAppBar(
              titleText: '随手拍历史',
            ),
            body: Container(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              margin: EdgeInsets.only(top: 16.w),
              child: CustomEasyRefreshList<RiskImgEntity>(
                dataLoad: controller.getData,
                itemBuilder: (RiskImgEntity item, int index) {
                  return CustomTitleChartPage(
                    title: item.securityControlRiskPointName,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        _getTextView(title: '作业活动/设备设施:', value: item.securityControlActivityName),
                        _getTextView(title: '上报时间:', value: item.createTime.isNotEmpty?Jiffy.parseFromMillisecondsSinceEpoch(int.parse(item.createTime)).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                        _getTextView(title: '隐患描述:', value: item.description, showSpacing: item.imgUrl.isNotEmpty),
                        _getImageView(url: item.imgUrl),
                        AppGaps.vGap20,
                        item.videoUrl.isNotEmpty ? Container(
                          height: 175.w,
                          alignment: Alignment.center,
                          child: VideoPlayWidget(
                            url: item.videoUrl ,
                          ),
                        ):Container(),
                      ],
                    ),
                  );
                },
              ),
            ),
          );
        });
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, bool showSpacing = true}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        Visibility(visible: showSpacing, child: AppGaps.vGap10),
      ],
    );
  }

  /// 获取图片展示
  Widget _getImageView({required String url}) {
    if (url.isEmpty) {
      return Container();
    }
    // 分号隔开的字符串转为list
    final urlList = url.split(';');
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '拍照/拍摄:',
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffC7CEE5),
          ),
        ),
        AppGaps.vGap8,
        ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              return Container(
                height: 173.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.w),
                  border: Border.all(color: Colors.white, width: 2.w),
                ),
                child: urlList[index].asImg(
                  clickShowLarge: true,
                  width: double.infinity,
                  fit: BoxFit.cover,
                  radius: 8.w,
                ),
              );
            },
            separatorBuilder: (context, index) {
              return AppGaps.vGap20;
            },
            itemCount: urlList.length)
      ],
    );
  }
}
